import sys

def str2html(a):
    ret = ""
    mp = {" ": "&nbsp;", "<": "&lt;", ">": "&gt;", "&": "&amp;", "\"": "&quot;", "'": "&apos;"}
    for i in a:
        if i in mp:
            ret += mp[i]
        else:
            ret += i
    return ret

_TEXT = 0
_IMAGE_HEAD = 1
_LEFT_BRACKET = 2
_RIGHT_BRACKET = 3
_LEFT_SQBRACKET = 4
_RIGHT_SQBRACKET = 5
_INLINE_FORMULA = 6
_FORMULA = 7
_QUOTE = 8, _QUOTE_END = -8
_TABLE = 9
_EXTBLOCK = 10
_SEPLINE = 11
_H = 12

# inline
_CODE = 13
_STRONG = 14
_ITALIC = 15
_DELETE = 16
_STRONGITALIC = 17

def warning(err, context = ""):
    if context == "":
        print("Warning " + err)
    else:
        print("Warning " + err + " Occured near " + context)

def error(err, context = ""):
    if context == "":
        print("Error " + err)
    else:
        print("Error " + err + " Occured near " + context)
    print("Aborting...")
    sys.exit(0)

def markdown(a):
    unit = []
    tmp = ""
    onTransfer = False

    if a[-2:] != "\n\n":
        warning("Text not end with an empty line.")
        if a[-1] == "\n":
            a = a + "\n"
        else:
            a = a + "\n\n"
    i = 0
    while i < len(a):
        onQuote = False
        def pushin(c):
            nonlocal tmp,unit
            if tmp != "":
                unit.append((_TEXT, tmp))
                tmp = ""
            unit.append(c)

        if onTransfer:
            onTransfer = False
            tmp += a[i]
            i += 1
            continue
        if a[i] == "\\":
            onTransfer = True
            i += 1
            continue
        mp = {"(": _LEFT_BRACKET, ")": _RIGHT_BRACKET, "|": _TABLE, "!": _IMAGE_HEAD, "[": _LEFT_SQBRACKET, "]": _RIGHT_SQBRACKET, ">": _QUOTE}
        if a[i] in mp:
            pushin((mp[a[i]], a[i]))
            if a[i] == ">":
                onQuote = True
            i += 1
            continue
        if a[i] == "$":
            if a[i+1] == "$":
                i = i + 2
                s = ""
                while a[i: i+2] != "$$":
                    s += a[i]
                    i += 1
                i += 2
                pushin((_FORMULA, s))
                continue
            else:
                s = ""
                while i > len(a) or a[i] != "$":
                    s += a[i]
                    i += 1
                pushin((_INLINE_FORMULA, s))
                i += 1
                continue
        if a[i] == "\n":
            if a[i+1] == "\n":
                if tmp != "":
                    unit.append((_TEXT, tmp))
                    tmp = ""
                i += 2
                if onQuote:
                    unit.append((_QUOTE_END, ""))
                    onQuote = False
                continue
            warning("No empty line between two texts.")
            i += 1
            continue
        if a[i] == "`":
            if a[i: i+3] == "```":
                typ = ""
                j = i + 3
                while j < len(a) and a[j] != "\n":
                    typ += a[j]
                    j += 1
                cont = ""
                while j < len(a) and a[j: j+4] != "```\n":
                    cont += a[j]
                    j += 1
                i = j + 4
                pushin((_EXTBLOCK, (typ, cont)))
                continue
            else:
                pushin((_CODE, a[i]))
                i += 1
                continue
        if a[i] == "#" and (i == 1 or a[i-1] == "\n"):
            cnt = i
            while a[i] == "#":
                i += 1
            pushin((_H, a[cnt: i]))
        if a[i: i+3] == "---":
            tmp = i
            while a[i] == "-":
                i += 1
            if a[i] == "\n":
                i += 1
                pushin((_SEPLINE, a[tmp: i]))
                continue
            else:
                i = tmp
        if a[i] == "_" or a[i] == "*":
            if a[i+1] == a[i]:
                if a[i+2] == a[i]:
                    pushin((_STRONGITALIC, a[i: i+3]))
                    i += 3
                else:
                    pushin((_STRONG, a[i: i+2]))
                    i += 2
            else:
                pushin((_ITALIC, a[i]))
                i += 1
            continue
        if a[i: i+2] == "~~":
            pushin((_DELETE, a[i: i+2]))
            i += 2
            continue
        tmp += a[i]
        i += 1

    ret = ""
    indx = 0
    while indx < len(unit):
        i = unit[indx]
        if i[0] == _TEXT:
            ret += "<p>%s</p>" % i[1]
        if i[0] == _IMAGE_HEAD:
            def testFmt(a):
                now = indx
                for iter in a:
                    now += 1
                    if unit[now][0] != iter:
                        return False
                return True
            if testFmt([_LEFT_SQBRACKET, _TEXT, _RIGHT_SQBRACKET, _LEFT_BRACKET, _TEXT, _RIGHT_BRACKET]):
                ret += '<img src="%s" alt="%s"/>' % (unit[indx + 5][1], unit[indx + 2][1])
                indx += 7
                continue
            else:
                error("Unrecognized image format.")
        if i[0] == _LEFT_BRACKET:
            if testFmt([_TEXT, _RIGHT_BRACKET, _LEFT_SQBRACKET, _TEXT, _RIGHT_SQBRACKET]):
                ret += '<a href="%s">%s</a>' % (unit[indx + 4][1], unit[indx + 1][1])
                indx += 6
                continue
            else:
                error("Unrecognized link.")
        if i[0] == _FORMULA:
            ret += "$$%s$$" % i[1]
        if i[0] == _INLINE_FORMULA:
            ret += "$%s$" % i[1]
        if i[0] == _QUOTE:
            ret += "<quote>"
        if i[0] == _QUOTE_END:
            ret += "</quote>"
        if i[0] == _EXTBLOCK:
            if i[0][1] in ["python", "C++"]:
                ret += "I don't know."
        if i[0] == _SEPLINE:
            ret += "<hr />"
        
